/*
 * Filename:    IntegrityLevelSetServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-3-17
 */
package com.paic.mhis.hcpms.trust.biz.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.quota.settlement.dao.KpiRegionCodeDAO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityLevelSetService;
import com.paic.mhis.hcpms.trust.dto.IntegrityIllegalDTO;
import com.paic.mhis.hcpms.trust.dto.IntegrityLevalSetMiddleDTO;
import com.paic.mhis.hcpms.trust.dto.IntegrityLevelDTO;
import com.paic.mhis.hcpms.trust.intergration.dao.IntegrityLevelSetDao;

@Component("integrityLevelSetServiceImpl")
public class IntegrityLevelSetServiceImpl implements IntegrityLevelSetService {
	@Autowired
	private IntegrityLevelSetDao integrityLevelSetDao;
	@Autowired
	private CommonDAO commonDao;

	@Autowired
	private KpiRegionCodeDAO kpiRegionCodeDao;

	/**
	 * 分页加载对应的关注级别信息
	 */
	@Override
	public List<IntegrityLevelDTO> loadIntegrityLevelDTO(Map<String, Object> parameter) {

		List<IntegrityLevelDTO> levelDataList = this.integrityLevelSetDao.loadIntegrityLevelDTO(parameter);

		if (null != levelDataList && levelDataList.size() > 0) {

			for (IntegrityLevelDTO bean : levelDataList) {
				String levelId = bean.getId();
				List<IntegrityIllegalDTO> illegalList = this.integrityLevelSetDao
						.loadIntegrityIllegalDTOByLevelId(levelId);
				bean.setList(illegalList);
			}
		}

		return levelDataList;
	}

	/**
	 * 处理保存的违规情节
	 */
	@Transactional
	@Override
	public void processSaveIntegrityLevelDTO(String levelId, UserDetails user, List<IntegrityIllegalDTO> list) {

		String regionCode = this.kpiRegionCodeDao.getRegionCode(user.getUserId());

		if (list != null && list.size() > 0) {

			for (IntegrityIllegalDTO currentBean : list) {

				IntegrityIllegalDTO bean = new IntegrityIllegalDTO();
				String flag = currentBean.getFlag();
				String isValid = currentBean.getIsValid();

				bean.setIlleal(currentBean.getIlleal());
				bean.setRegionCode(regionCode);
				bean.setOpertionUser(user.getUsername());
				bean.setUpdateUser(user.getUsername());

				if ("0".equals(flag) && "0".equals(isValid)) { // 新增的
					String uuid = this.commonDao.getUUID();
					bean.setId(uuid);
					bean.setCreateUser(user.getUsername());

					IntegrityLevalSetMiddleDTO middleDTO = new IntegrityLevalSetMiddleDTO();
					middleDTO.setCreateUser(user.getUsername());
					middleDTO.setIllegalId(uuid);
					middleDTO.setLevalId(levelId);
					middleDTO.setOpertionUser(user.getUsername());
					middleDTO.setRegionCode(regionCode);
					middleDTO.setUpdateUser(user.getUsername());

					this.integrityLevelSetDao.saveIntegrityIllealBean(bean);
					this.integrityLevelSetDao.saveIntegrityLevalSetMiddleDTO(middleDTO);

				} else if ("1".equals(isValid)) { // 删除的

					String illegalId = currentBean.getId();
					this.integrityLevelSetDao.deleteIllegalInfo(illegalId);
					this.integrityLevelSetDao.deleteLevelAndIlleaglMid(levelId, illegalId);

				} else {

					bean.setId(currentBean.getId());
					this.integrityLevelSetDao.updateHcpmsInteIllegalById(bean);

				}

			}
		}

	}

	/**
	 * 判断当前的违规情节是否可以删除掉
	 */
	@Override
	public boolean checkIlleaglInfoExist(String levelId, String illegalId) {

		boolean flag = this.integrityLevelSetDao.checkIntegrityLevelIsExist(levelId, illegalId);
		return flag;
	}

	/**
	 * 删除当前的违规情节
	 */
	@Override
	public void deleteIlleaglInfo(String levelId, String illegalId) {
		// TODO Auto-generated method stub

	}

	@Override
	public long loadIntegrityLevelDTOCount(Map<String, Object> parameter) {
		long count = this.integrityLevelSetDao.loadIntegrityLevelDTOCount(parameter);
		return count;
	}
}
